package com.platform.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.platform.annotation.LoginUser;
import com.platform.entity.ApiFollowVo;
import com.platform.entity.UserInfo;
import com.platform.entity.UserVo;
import com.platform.service.ApiUserService;
import com.platform.service.ApiZbIntimacyService;
import com.platform.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.platform.service.ApiFollowService;
import com.platform.utils.R;

/**
 * 关注Controller
 *
 * @author zhurusen
 * @email 524227263@qq.com
 * @date 2018-05-15 15:50:14
 */
@RestController
@RequestMapping("api/follow")
public class ApiFollowController {
    @Autowired
    private ApiFollowService apiFollowService;
    @Autowired
    private ApiUserService apiUserService;
    @Autowired
    private ApiZbIntimacyService apiZbIntimacyService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@LoginUser UserVo loginUser, @RequestParam(value = "page", defaultValue = "1") Integer page) {
        //查询列表数据
        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("limit", 10);
        params.put("cmUserId", loginUser.getId());

        params.put("sidx", "id");
        params.put("order", "desc");
        Query query = new Query(params);

        List<ApiFollowVo> reList = new ArrayList<>();
        List<ApiFollowVo> list = apiFollowService.queryList(query);

        for (ApiFollowVo info : list) {

            UserInfo userInfo = apiUserService.toUserInfo(info.getZbUserId(), loginUser);
            if (userInfo != null) {
                UserInfo vo = new UserInfo();
                vo.setId(userInfo.getId());
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickName(userInfo.getNickName());

                params = new HashMap<>();

                params.put("zbUserId", info.getZbUserId());
                params.put("cmUserId", loginUser.getId());

                List<Map<String, Object>> listMap = apiZbIntimacyService.queryBanking(params);
                if (listMap.size() > 0) {
                    vo.setIntimacy(Integer.valueOf(listMap.get(0).get("count").toString()));
                } else {
                    vo.setIntimacy(0);
                }


                vo.setOnlineStatus(userInfo.getOnlineStatus());
                info.setZbUser(vo);
                reList.add(info);
            }
        }


        return R.ok(0, "获取成功", reList);
    }

    /**
     * 关注
     *
     * @param loginUser
     * @param userId    主播id
     * @return
     */
    @RequestMapping("/addFollow")
    public R addFollow(@LoginUser UserVo loginUser, Long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("zbUserId", userId);
        params.put("cmUserId", loginUser.getId());
        List<ApiFollowVo> apiFollowList = apiFollowService.queryList(params);
        if (apiFollowList.size() > 0) {
            return R.error(1, "已关注");
        }
        ApiFollowVo info = new ApiFollowVo();
        info.setCmUserId(loginUser.getId());
        info.setZbUserId(userId);

        apiFollowService.save(info);

        params = new HashMap<>();
        params.put("zbUserId", userId);
        Map<String, Object> map = new HashMap<>();
        map.put("zbfollow", apiFollowService.queryTotal(params));

        params = new HashMap<>();
        params.put("cmUserId", loginUser.getId());
        map.put("cmfollow",  apiFollowService.queryTotal(params));

        return R.ok(0, "成功关注", map);
    }

    /**
     * 取消关注
     *
     * @param loginUser
     * @param userId    主播id
     * @return
     */
    @RequestMapping("/cancelFollow")
    public R cancelFollow(@LoginUser UserVo loginUser, Long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("zbUserId", userId);
        params.put("cmUserId", loginUser.getId());
        List<ApiFollowVo> apiFollowList = apiFollowService.queryList(params);
        if (apiFollowList.size() > 0) {
            ApiFollowVo info = apiFollowList.get(0);
            apiFollowService.del(info.getId());
        }

        params = new HashMap<>();
        params.put("zbUserId", userId);
        Map<String, Object> map = new HashMap<>();
        map.put("zbfollow", apiFollowService.queryTotal(params));

        params = new HashMap<>();
        params.put("cmUserId", loginUser.getId());
        map.put("cmfollow",  apiFollowService.queryTotal(params));

        return R.ok(0, "成功取消", map);
    }
}
